using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Appr;
using net_work.Data.Entity.Appr;
using net_work.IRepository.Appr;
using net_work.IService.Appr;

namespace net_work.Service.Appr;

public class FlowService(
    IFlowRepository flowRepository)
    : IFlowService
{
    public Task<PagedResult<Flow>> GetFlowsPagedAsync(PagedRequest request)
    {
        return flowRepository.GetPagedAsync(request);
    }

    public Task<Flow?> SelectByIdAsync(long flowId)
    {
        return flowRepository.GetByIdAsync(flowId);
    }

    public async Task<long> InsertFlow(FlowAddRequest flowAddRequest)
    {
        return await flowRepository.InsertAsync(flowAddRequest) > 0 ? 1 : -1;
    }

    public async Task<int> UpdateFlow(FlowUpdateRequest flowUpdateRequest)
    {
        if (flowUpdateRequest.Id <= 0)
        {
            return -1;
        }

        return await flowRepository.UpdateAsync(flowUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteFlowByIds(List<long> ids)
    {
        return await flowRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }

    public async Task<int> ReleaseFlow(long flowId)
    {
        if (await flowRepository.ValidateFlowAsync(flowId))
        {
            return await flowRepository.ReleaseFlow(flowId) ? 1 : -1;
        }

        return -1;
    }
}